#!/usr/bin/env python3
#

from flask_admin import BaseView, expose
from flask_admin.contrib.sqla import ModelView
from flask import redirect, session, g, flash, url_for
from flask_login import current_user


class CustomView(BaseView):
    """View function of Flask-Admin for Custom Page."""

    @expose('/')
    def index(self):
        if current_user.is_authenticated:
            return self.render('admin/custom.html')
        else:
            return redirect(url_for('blog.home'))

    @expose('second_page')
    def second_page(self):
        return self.render('admin/second_page.html')


class CustomModelView(ModelView):
    """View function of flask_admin for model page."""

    def is_accessible(self):
        return current_user.is_authenticated

    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('blog.home', next=request.url))